//
//  smcUpdateCheck.m
//  juhu
//
//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

// checks for newer versions

#import "smcUpdateCheck.h"

//#define UPDATE_URL @"http://code.google.com/p/juhu/wiki/test"
#define UPDATE_URL @"http://code.google.com/p/juhu/wiki/latest"
#define WEB_URL @"http://juhu-mac.blogspot.com"
#define DOWNLOAD_BASE_URL @"http://juhu.googlecode.com/files/"
#define FILE_NAME @"juhu"
#define EXTENSION @".zip"

@implementation smcUpdateCheck

- (void) dealloc 
{
	[connection release];
	[super dealloc];
}

- (id) init 
{
	//do init stuff
	self = [super init];
	if (self != nil) 
	{
		// set up connection, delegate to self
		connection = [[smcAsyncConnection alloc] init];
		[connection setDelegate: self];
	} else
		connection = nil;
	return self;
}

- (void) checkForNewerVersion
{
	//starts the check
	//kick off download from URL
	[connection beginDownloadFromURL: [NSURL URLWithString: UPDATE_URL]
		withTimout: 5 userAgent: JUHUCLIENT_VERSION andInfo: nil];
}

- (void) downloadFromURL: (NSURL *) url didFinishWithData: (NSData *) data andInfo: (id) info
{
	//delegated from connection
	//download okay, compare version string with bundle one

	NSMutableString *tmp;

	if (data == nil)
		return;
	tmp = [[[NSMutableString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
	if (tmp == nil)
		return;

	//get version number
	NSRange range = [tmp rangeOfString: @"latestVersion=" options: NSBackwardsSearch range: NSMakeRange(0, [tmp length])];

	if (range.location == NSNotFound)
		return;

	[tmp deleteCharactersInRange: NSMakeRange( 0, range.location + range.length)];
	NSString *latestVersion = [tmp substringToIndex: 5];
	NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleVersion"];
	
	//and make numbers	
	int thisOne = [[version substringToIndex: 1] intValue] *100 + [[version substringWithRange: NSMakeRange( 2, 1)] intValue] * 10
		+ [[version substringWithRange: NSMakeRange( 4, 1)] intValue];
	int remoteOne = [[latestVersion substringToIndex: 1] intValue] *100 + [[latestVersion substringWithRange: NSMakeRange( 2, 1)] intValue] * 10
		+ [[latestVersion substringWithRange: NSMakeRange( 4, 1)] intValue];

	if (thisOne < remoteOne)
	{
		//update!
		int todo = NSRunAlertPanel( @"Newer Version available", 
			[NSString stringWithFormat: @"You are currently using juhu %@, there is a more recent version (%@) available.\nYou may want to go to the juhu web site or download it directly.", version, latestVersion],
				@"Goto juhu-mac.blogspot.com", @"Remind me later", @"Download");

		if (todo == -1)
		{
			//download
			//built url
			NSMutableString *ver = [NSMutableString stringWithString: latestVersion];
			[ver replaceOccurrencesOfString: @"." 
				withString: @"" options: NSBackwardsSearch range: NSMakeRange( 0,[latestVersion length])];

			NSURL *downloadURL = [NSURL URLWithString: 
				[NSString stringWithFormat: @"%@%@%@%@", 
					DOWNLOAD_BASE_URL,
					FILE_NAME,
					ver,
					EXTENSION]];
			//open url
			[[NSWorkspace sharedWorkspace] openURL: downloadURL];
		}
		else if (todo == 1)
		{
			//goto web site
			[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: WEB_URL]];
		}
	}

}

- (void) downloadFromURL: (NSURL *) url didFailWithError: (NSError *) error andInfo: (id) info
{
	//something went wrong
	//nothing todo here, because its just an update check, right?
}

@end
